import json
from pprint import pprint

import pymysql
import requests
import csv

url = 'https://dncapi.moveft.com/api/coin/web-charts?code=bitcoin&type=ydt&webp=1'

request = requests.get(url=url)
json_data = request.json()['value']
# print(json_data)
for i in json.loads("[" + json_data + "]"):
    # pprint(i)
    belong_time=i[0]
    price_now=i[1]

    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='password', database='finance')
    cursor = conn.cursor()

    sql=f"""insert into myapp_trendhistory values (0,'{belong_time}','{price_now}','2')"""
    try:
        cursor.execute(sql)
        conn.commit()
        print('插入成功')
    except Exception as e:
        print('插入失败',e)
        conn.rollback()